package com.mrx.xfriendship.model.dto;

import com.alibaba.fastjson.JSON;
import com.mrx.commons.config.Constant;
import com.mrx.commons.util.LambdaUtil;
import lombok.Data;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * /error 路由异常包装类
 *
 * @author : 肖康宁
 * @since : 2022-12-09 14:16
 **/
@Data
public class ErrorInfoDTO implements Serializable {

    /**
     * 请求方法
     */
    private String requestMethod;

    /**
     * 请求 url
     */
    private String requestUrl;

    /**
     * 请求 状态码
     */
    private String statusCode;

    public static ErrorInfoDTO of(HttpServletRequest request) {
        return LambdaUtil.withBean(ErrorInfoDTO.class, dto -> {
            dto.setRequestUrl(request.getAttribute(Constant.ERROR_REQUEST_URI).toString());
            dto.setStatusCode(request.getAttribute(Constant.ERROR_STATUS_CODE).toString());
            dto.setRequestMethod(request.getMethod());
        });
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

}
